package com.jinbo.footmessageworker.person;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.jinbo.footmessageworker.R;
import com.jinbo.footmessageworker.httpbean.ProjectBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jinbo on 2015/8/19.
 */
public class CheckItemListViewAdapter extends BaseAdapter {

    public interface Callbacks {
        public void change(int[] numbers);
    }

    static class ViewHolder {
        TextView tv_name, tv_duration, tv_price;
        CheckBox check_box;
    }

    List<ProjectBean> projects;
    ArrayList<Integer> ids;
    private Context context;
    private Callbacks mCallBacks;
    int[] new_ids;
    public CheckItemListViewAdapter(Context context, List<ProjectBean> dataList, ArrayList<Integer> idlist) {//ArrayList<ProjectsBean> datalist) {
        this.context = context;
        this.projects = dataList;
        this.ids = idlist;
        mCallBacks = (Callbacks) context;

    }

    void setprojects( List<ProjectBean> dataList) {
        this.projects = dataList;
      //  if(dataList!=null) {
            new_ids = new int[dataList.size()];
            for (int i = 0; i < dataList.size(); i++)
                new_ids[i] = -1;
        //}
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub*//*
        if (projects != null) {
            return projects.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        if (projects != null) {
            return projects.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override

    public View getView(final int position, View convertView, ViewGroup arg2) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_kind_check, null);
            holder = new ViewHolder();
            holder.tv_name = (TextView) convertView.findViewById(R.id.name);
            holder.tv_duration = (TextView) convertView.findViewById(R.id.duration);
            holder.tv_price = (TextView) convertView.findViewById(R.id.price);
            holder.check_box = (CheckBox) convertView.findViewById(R.id.check_box);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tv_name.setText(projects.get(position).name);
        holder.tv_duration.setText(projects.get(position).duration+"分钟");
        holder.tv_price.setText(projects.get(position).price+"元");
        for (int i = 0; i < ids.size(); i++) {
            if (ids.get(i) == projects.get(position).id) {
                holder.check_box.setChecked(true);
                new_ids[position]=projects.get(position).id;
            }
        }
        holder.check_box.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                 if(isChecked)
                     new_ids[position]=projects.get(position).id;
                 else
                     new_ids[position]=-1;
                mCallBacks.change(new_ids);
            }
        });
        return convertView;
    }
}
